package android.slc.icr.core.utils;

import android.slc.icr.core.constant.IcrConstants;
import android.slc.icr.core.entity.IcrMessage;
import android.slc.icr.service.IcrMessageRemoteService;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

public class IcrMessageUtils {

    //------------------------------------------------检验必要参数start------------------------------//

    public static boolean checkIcrMessage(IcrMessage icrMessage) {
        String sendId = icrMessage.getSender();
        //发送者id为空直接丢弃
        if (StrUtil.isEmpty(sendId)) {
            return false;
        }
        String action = icrMessage.getAction();
        String roomId = icrMessage.getRelateId();
        //事件和房间id为空时直接返回缺少参数
        if (StrUtil.isEmpty(action) || StrUtil.isEmpty(roomId)) {
            return false;
        }
        return true;
    }
    //------------------------------------------------检验必要参数end------------------------------//

    //------------------------------------------------发送错误消息start------------------------------//
    public static final String STR_DOES_NOT_EXIST = "聊天室不存在";
    public static final String STR_NOT_IN_ROOM = "你在这个房间";

    /**
     * 非法的
     * @param icrMessageRemoteService
     * @param action
     * @param content
     * @param baseIcrMessage
     */
    public static void sendChartRoomIllicit(IcrMessageRemoteService icrMessageRemoteService, String action, String content, IcrMessage baseIcrMessage) {
        baseIcrMessage.setAction(action + IcrConstants.Action.ACTION_PARAM_ILLICIT_SUFFIX);
        baseIcrMessage.setContent(content);
        icrMessageRemoteService.sendMessage(baseIcrMessage);
    }

    /**
     * 没有权限
     * @param icrMessageRemoteService
     * @param action
     * @param baseIcrMessage
     */
    public static void sendChartRoomPermissionDenied(IcrMessageRemoteService icrMessageRemoteService, String action, IcrMessage baseIcrMessage) {
        sendChartRoomPermissionDenied(icrMessageRemoteService, action, "没有权限", baseIcrMessage);
    }

    /**
     * 没有权限
     * @param icrMessageRemoteService
     * @param action
     * @param content
     * @param baseIcrMessage
     */
    public static void sendChartRoomPermissionDenied(IcrMessageRemoteService icrMessageRemoteService, String action,
                                                     String content, IcrMessage baseIcrMessage) {
        baseIcrMessage.setAction(action + IcrConstants.Action.ACTION_PARAM_ILLICIT_SUFFIX);
        baseIcrMessage.setContent(content);
        icrMessageRemoteService.sendMessage(baseIcrMessage);
    }

    /**
     * 缺少参数的
     * @param icrMessageRemoteService
     * @param action
     * @param baseIcrMessage
     */
    public static void sendActionMissingParam(IcrMessageRemoteService icrMessageRemoteService, String action, IcrMessage baseIcrMessage) {
        sendActionMissingParam(icrMessageRemoteService, action, "缺少参数", baseIcrMessage);
    }

    /**
     * 缺少参数的
     * @param icrMessageRemoteService
     * @param action
     * @param baseIcrMessage
     */
    public static void sendActionMissingParam(IcrMessageRemoteService icrMessageRemoteService, String action, String content, IcrMessage baseIcrMessage) {
        baseIcrMessage.setAction(action + IcrConstants.Action.ACTION_PARAM_MISSING_SUFFIX);
        baseIcrMessage.setContent(content);
        icrMessageRemoteService.sendMessage(baseIcrMessage);
    }

    //------------------------------------------------发送错误消息end------------------------------//

    //------------------------------------------------额外数据start------------------------------//


    //------------------------------------------------额外数据end------------------------------//

    /**
     * 获取timeId
     */
    public static String createTimeId() {
        return Long.toHexString(System.currentTimeMillis() * (1 + RandomUtil.randomInt(100)))
                .substring(0, 7);
    }
}
